<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

    /**
     * Constructor de la clase makinita.
     * @param array $entrada
     * @param array $reglas
     * @return boolean $error
     */

     
class sfTablePager {
    
    private $name;
    private $text;
    private $style;
    private $htmlClass;
    private $parent;
    
    
    private $query;
    private $pageSize = 10;
    private $recordCount = 0;    
    private $pageCount = 0;
    private $page = 1;
    private $count = 0;
    

    public function __construct($style = null ,$htmlClass = null ) {

        //$this->name = $name;
        $this->style = $style;
        $this->htmlClass = $htmlClass;        
        
    }

    public function bind($parameters) {
        
        if(isset($parameters['page']))
            if($parameters['page']>=1) {
                $this->page = $parameters['page'];
            }else {
                $this->page = 1;
            }
        else
            $this->page = 1;
        
        if(isset($parameters['sig']))
            $this->page++; 

        if(isset($parameters['prev']) && $this->page > 1)
            $this->page--; 

        
    }
    
    
    public function setQuery($query) {
        $this->query = $query;
    }
    
    public function setParent($parent) {
        $this->parent = $parent;
    }

    
    public function execute() {

        
        $this->pageSize = $this->parent->getPageSize();
        
        
        $queryAux = $this->query->copy();
        $queryAux->select('Count(*)');
        
        $this->recordCount =  $queryAux->execute(array(),Doctrine_Core::HYDRATE_SINGLE_SCALAR);
        
        $this->pageCount = ceil($this->recordCount/$this->pageSize);
        
        if(($offset = ($this->page - 1)  * $this->pageSize)<$this->recordCount)
        {
            $this->query->offset($offset);
        }else
        {
            if(($this->page = $this->pageCount)==0)$this->page =1;
            $this->query->offset(($this->page - 1)  * $this->pageSize);            
        }
        $this->query->limit($this->pageSize);
        
        return $this->query;
    }
    
    public function getName() {
        return $this->name;
    }

    public function render() {

        $inputName = $this->parent->getName().'Values[page]';
        
        $result = '<tr style = "'.$this->style.'" class = "'.$this->htmlClass.'" >';
        
        $result .=      '<td><label>'.$this->recordCount.' items en '.$this->pageCount.' páginas</label></td>'. 
                        '<td style="width: 60px;">  <input type="submit" style="float: right;" value="<<" name="'.$this->parent->getName().'Values[prev]"></td>'.
                        '<td> Pág '.$this->page.' </td>'.                
                        '<td style="width: 60px;">  <input type="submit" style="float: left;" value=">>" name="'.$this->parent->getName().'Values[sig]"></td>'.                            
                        '<td>Ir a página  <input style="text-align:right;" type="text" name = '.$inputName.' size="3" value="'.$this->page.'" ></td>'.                 
                        '<td><input type="submit" value="Ir"></td>'; 
        
        

        $result = $result.'</tr>';
        
        return $result;
        
    }
    
}


?>
